#include "admin.h"
#include "modify.h"
#include "ui_modify.h"
#include "ui_admin.h"
#include <QSqlQuery>
#include <QDebug>
#include <QMessageBox>
Modify::Modify(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Modify)
{
    ui->setupUi(this);

    //    qDebug() << QSqlDatabase::drivers();
    //    m_db = QSqlDatabase::addDatabase("QMYSQL");
    //    m_db.setHostName("localhost");//主机名
    //    m_db.setPort(3306);
    //    m_db.setDatabaseName("user");
    //    m_db.setUserName("root");
    //    m_db.setPassword("123456");
    //    m_db.open();
    //    if(m_db.open())
    //    {
    //        qDebug() << "连接成功";
    //    }
    //    else
    //    {
    //        qDebug() << "连接失败！！";
    //    }
}

Modify::~Modify()
{
    delete ui;
}

void Modify::on_pushButtonBack_clicked()
{
    Admin* m = new Admin();
    m->show();
    this->close();
}
//void Modify::receiveData(QString data1, QString data2)
//{
//    ui->NewUserName->setText(data1);
//    ui->NewPassword->setText(data2);
//}
//修改
void Modify::on_pushButtonOk_clicked()
{

    QString Newusername = ui ->NewUserName ->text();
    QString Newpassword = ui ->NewPassword ->text();

    QSqlQuery query;

    query.prepare("UPDATE users SET UserName=?, Password=? WHERE UserName=?");
    query.addBindValue(Newusername);
    query.addBindValue(Newpassword);
    query.addBindValue(Newusername);
    if(query.exec())
    {
        qDebug() << "连接成功";
        QMessageBox::warning(this, "成功！", "修改成功！！！");
    }
    else
    {
        qDebug() << "连接失败！！";
    }
    ui->NewUserName->clear();
    ui->NewPassword->clear();

}

